package com.carpedil.demo.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.carpedil.demo.po.Permission;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

/** @author Administrator */
@Repository
public interface PermissionMapper extends BaseMapper<Permission> {

  /**
   * getUserPermissions
   *
   * @param userId userId
   * @return List<Permission>
   */
  @Select(
      "select DISTINCT p.*"
          + " FROM user_role ur "
          + " INNER JOIN users u on u.id = ur.fk_user_id "
          + " INNER JOIN roles r on r.pk_id = ur.fk_role_id "
          + " INNER JOIN role_permission rp on rp.fk_role_id = r.pk_id "
          + " INNER JOIN permissions p on p.permission_pk_id = rp.fk_permission_id "
          + " WHERE pid = 0 and u.id = #{userId}")
  @ResultMap("com.carpedil.demo.dao.PermissionMapper.permissionMap")
  List<Permission> getUserPermissions(@Param("userId") Integer userId);
}
